using AutoMapper;
using Blog.Repository.Permission;
using Blog.Service.Permission;
using Blog.Service.Permission.Output;
using XjjXmm.Infrastructure.Repository;
using XjjXmm.Infrastructure.User;

namespace Blog.Service;



/// <summary>
/// 权限
///</summary>
public class PermissionService  : CrudService<PermissionModel, PermissionEntity>, IPermissionService
{
    private readonly IPermissionRepository _permissionRepository;

    public PermissionService(IPermissionRepository repository, IUser user, IMapper mapper) : base(repository, user, mapper)
    {
        _permissionRepository = repository;
    }

    public async Task<List<PermissionListOutput>> GetList(string? key, DateTime? start, DateTime? end)
    {
        var entity = await _permissionRepository.GetAll();

        var dto = _mapper.Map<List<PermissionListOutput>>(entity);

        return dto;
    }

    public async Task<IEnumerable<PermissionEntity>> GetMenuPermissionByUserId(long userId)
    {
          return await _permissionRepository.GetMenuPermissionByUserId(userId);
    }

    public async Task<object> GetPermissionList()
    {
        /* var permissions = await _permissionRepository.Select
             .WhereIf(_appConfig.Tenant && User.TenantType == TenantType.Tenant, a =>
                 _tenantPermissionRepository
                 .Where(b => b.PermissionId == a.Id && b.TenantId == User.TenantId)
                 .Any()
             )
             .OrderBy(a => a.ParentId)
             .OrderBy(a => a.Sort)
             .ToList(a => new { a.Id, a.ParentId, a.Label, a.Type });

         var apis = permissions
             .Where(a => a.Type == PermissionType.Dot)
             .Select(a => new { a.Id, a.ParentId, a.Label });

         var menus = permissions
             .Where(a => (new[] { PermissionType.Group, PermissionType.Menu }).Contains(a.Type))
             .Select(a => new
             {
                 a.Id,
                 a.ParentId,
                 a.Label,
                 Apis = apis.Where(b => b.ParentId == a.Id).Select(b => new { b.Id, b.Label })
             });

         return menus;*/
        return await _permissionRepository.GetPermissionList();
    }
}